package com.yequan.thread.future;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * @author yequan
 * @date 2021/08/04 16:56
 **/
public class FutureTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        simple1();
        simple2();
    }

    private static void simple2() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        long start = System.currentTimeMillis();
        Callable<Clothes> callable = () -> {
            Clothes clothes = new Clothes();
            clothes.washClothes();
            return clothes;
        };
        List<Future<Clothes>> futureList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Future<Clothes> future = threadPool.submit(callable);
            futureList.add(future);
        }
        Dishes dishes = new Dishes();
        dishes.washDishes();
        List<Clothes> numList = new ArrayList<>();
        for (Future<Clothes> clothesFuture : futureList) {
            numList.add(clothesFuture.get());
        }
        if (numList.size() == 3) {
            System.out.println("===========over===========");
        }
        long end = System.currentTimeMillis();
        System.out.println("all tasks cost " + (end - start) + " millis");
        threadPool.shutdown();
    }

    private static void simple1() {
        long start = System.currentTimeMillis();
        Callable<Clothes> callable = () -> {
            Clothes clothes = new Clothes();
            clothes.washClothes();
            return clothes;
        };
        FutureTask<Clothes> futureTask = new FutureTask<Clothes>(callable);
        new Thread(futureTask).start();
        Dishes dishes = new Dishes();
        dishes.washDishes();
        Clothes clothes = null;
        try {
            clothes = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        if (clothes != null) {
            System.out.println("===========over===========");
        }
        long end = System.currentTimeMillis();
        System.out.println("all tasks cost " + (end - start) + " millis");
    }

    static class Clothes {
        public void washClothes() {
            System.out.println("machine starts to wash cloths");
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("all clothes are cleaned!!!");
        }
    }

    static class Dishes {
        public void washDishes() {
            System.out.println("start to wash dishes");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("all dishes are cleaned!!!");
        }
    }

}
